<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 27.3</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="27.2.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="27.3.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P4">Part IV. The C API</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#27">Chapter 27. Techniques for Writing C Functions</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><h2>27.3 &ndash; Storing State in C Functions</h2>

<p>Frequently, C functions need to keep some non-local data,
that is, data that outlive their invocation.
In C, we typically use global or static variables for that need.
When you are programming library functions for Lua, however,
global and static variables are not a good approach.
First, you cannot store a generic Lua value in a C variable.
Second, a library that uses such variables cannot be used
in multiple Lua states.

<p>An alternative approach is to store such values into Lua global variables.
This approach solves the two previous problems.
Lua global variables store any Lua value
and each independent state has its own independent set of global variables.
However, this is not always a satisfactory solution,
because Lua code can tamper with those global variables
and therefore compromise the integrity of C data.
To avoid this problem, Lua offers a separate table,
called the <em>registry</em>, that C code can freely use,
but Lua code cannot access.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="27.3.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

